﻿Public Class FrmDepEnCours
    Dim macnx As New PPE3.ClassMySqlConnect("853339_depanneur", "071064")
    Dim nbchamps As Integer = 0
    Dim position As Integer = 0
    Dim idcli As Integer
    Dim TempsAtelier As Date
    Dim TempsLimite As Date
    Dim Message, Titre As String
    Dim stat As Boolean

    Private Sub FrmDepEnCours_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        Me.MdiParent = FrmStart
        Me.Top = 0
        Me.Left = 0
        connection()

        rafraichir()
    End Sub

#Region "Fonction"

    Public Function RemplaceSlashApostrophesDataTable(ByRef lesdonnees As DataTable)
        Dim nblignes As Integer
        Dim nbcolonnes As Integer
        Dim test As String = ""
        nblignes = lesdonnees.Rows.Count
        nbcolonnes = lesdonnees.Columns.Count
        Dim i, j As Integer
        For i = 0 To nblignes - 1
            For j = 0 To nbcolonnes - 1
                'Attention le replace ne s'applique qu'aux type String : utilisation de la classe de base Object :
                If Object.ReferenceEquals(lesdonnees.Rows(i).Item(j).GetType(), test.GetType()) Then
                    lesdonnees.Rows(i).Item(j) = RemplaceSlashApostrophes(lesdonnees.Rows(i).Item(j))
                End If
            Next
        Next
        Return lesdonnees
    End Function

    Public Function RemplaceApostrophesSlash(ByRef unechaine As String) As String
        Dim ch As String
        ch = unechaine.Replace("'", "/")
        Return ch
    End Function

    Public Function RemplaceSlashApostrophes(ByRef unechaine As String) As String
        Dim ch As String
        ch = unechaine.Replace("/", "'")
        Return ch
    End Function

    Public Sub rafraichir()
        Dim ligne As Integer
        Dim i As Integer = 0
        stat = True
        If DataGridListeDep.RowCount - 1 <> 0 Then

            lblIdIntervention.Text = DataGridListeDep.CurrentRow.Cells(0).Value.ToString()
            TextBoxNomClient.Text = DataGridListeDep.CurrentRow.Cells(1).Value.ToString()
            TextBoxPrenomClient.Text = DataGridListeDep.CurrentRow.Cells(2).Value.ToString()
            TextBoxTypInt.Text = DataGridListeDep.CurrentRow.Cells(3).Value.ToString()
            TextBoxTypPc.Text = DataGridListeDep.CurrentRow.Cells(4).Value.ToString()
            TextBoxLieu2.Text = DataGridListeDep.CurrentRow.Cells(5).Value.ToString()
            DateTimePickRecept.Text = DataGridListeDep.CurrentRow.Cells(6).Value.ToString()
            lblidClient.Text = DataGridListeDep.CurrentRow.Cells(10).Value.ToString()

            ligne = DataGridListeDep.RowCount - 1
            i = 0

            If DataGridListeDep.CurrentRow.Cells(7).Value.ToString() <> "" Then
                Dim DateDebut As Date = DataGridListeDep.CurrentRow.Cells(7).Value.ToString()
            End If

            GroupBoxEtat.Visible = True
            Label7.Text = "Temps de l'intervention :"

            If DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("en reception") Or DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("en cours") Then
                CheckBoxEnCour.Checked = True

                CheckBoxEnDepannage.Checked = False
                CheckBoxAttenteC.Visible = True
                CheckBoxAttenteP.Checked = False
                CheckBoxAttenteC.Checked = False
                CheckBoxFinDepannage.Checked = False
                CheckBoxEnDepannage.Enabled = True
                CheckBoxAttenteP.Enabled = True
                CheckBoxAttenteC.Enabled = True
                CheckBoxFinDepannage.Enabled = True
            End If

            If DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("en depannage") Or DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("en attente piece") Or DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("en attente confirmation") Then
                CheckBoxEnCour.Checked = False
                CheckBoxEnDepannage.Enabled = False
                CheckBoxEnDepannage.Checked = True
                CheckBoxFinDepannage.Checked = False
                CheckBoxFinDepannage.Enabled = True
                CheckBoxAttenteP.Enabled = True
                CheckBoxAttenteC.Enabled = True
            ElseIf DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("en attente piece") Then
                CheckBoxAttenteC.Checked = False
                CheckBoxAttenteP.Checked = True
            ElseIf DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("en attente confirmation") Then
                CheckBoxAttenteP.Checked = False
                CheckBoxAttenteC.Checked = True
            ElseIf DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("deplacement") Or DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("vente") Then
                GroupBoxEtat.Visible = False
                Label7.Text = "Date RDV :"
                TextBoxTempsIntervention.Text = DataGridListeDep.CurrentRow.Cells(7).Value.ToString()


            ElseIf DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = "fin depannage" Then
                CheckBoxEnCour.Checked = False
                CheckBoxEnDepannage.Enabled = False
                CheckBoxEnDepannage.Checked = False
                CheckBoxFinDepannage.Enabled = False
                CheckBoxFinDepannage.Checked = True
                CheckBoxAttenteC.Enabled = False
                CheckBoxAttenteC.Checked = False
                CheckBoxAttenteP.Enabled = False
                CheckBoxAttenteP.Checked = False
            End If
        End If
        If (DataGridListeDep.CurrentRow.Cells(5).Value.ToString() = "domicile") Or (DataGridListeDep.CurrentRow.Cells(3).Value.ToString() = "formation") Or DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("vente") Then

        Else
            If lblIdIntervention.Text <> "..." Then
                If DataGridListeDep.CurrentRow.Cells(7).Value.ToString() <> "" Then
                    macnx.Se_Connecter_MySqlServer()
                    If DataGridListeDep.CurrentRow.Cells(8).Value.ToString() <> "" Then
                        TextBoxTempsIntervention.Text = macnx.Executer_Requete_TIMEDIFF_Finis(lblIdIntervention.Text, "DateHeureFinIntervention", "DateHeureDebIntervention")
                    Else
                        TextBoxTempsIntervention.Text = macnx.Executer_Requete_TIMEDIFF_EnCour(lblIdIntervention.Text, "DateHeureDebIntervention")
                    End If
                    macnx.Se_Deconnecter_MySqlServer()
                Else
                    TextBoxTempsIntervention.Text = "00.00.00"
                End If

            End If
        End If
        If (DataGridListeDep.CurrentRow.Cells(5).Value.ToString() = "domicile") Or (DataGridListeDep.CurrentRow.Cells(3).Value.ToString() = "formation") Or DataGridListeDep.CurrentRow.Cells(11).Value.ToString() = ("vente") Then

        Else
            If lblIdIntervention.Text <> "..." Then
                If DataGridListeDep.CurrentRow.Cells(6).Value.ToString() <> "" Then
                    macnx.Se_Connecter_MySqlServer()
                    If DataGridListeDep.CurrentRow.Cells(8).Value.ToString() <> "" Then
                        TextBoxOrdiAtelier.Text = macnx.Executer_Requete_TIMEDIFF_Finis(lblIdIntervention.Text, "DateHeureFinIntervention", "DateHeureReception")
                    Else
                        TextBoxOrdiAtelier.Text = macnx.Executer_Requete_TIMEDIFF_EnCour(lblIdIntervention.Text, "DateHeureReception")
                    End If
                    macnx.Se_Deconnecter_MySqlServer()
                End If
            End If
        End If
        While (i < ligne)
            If DataGridListeDep.Rows(i).Cells(11).Value.ToString() = "en reception" Then
                DataGridListeDep.Rows(i).DefaultCellStyle.BackColor = Color.Red
            ElseIf DataGridListeDep.Rows(i).Cells(11).Value.ToString() = "en cours" Then
                DataGridListeDep.Rows(i).DefaultCellStyle.BackColor = Color.Red
            ElseIf DataGridListeDep.Rows(i).Cells(11).Value.ToString() = "en depannage" Then
                DataGridListeDep.Rows(i).DefaultCellStyle.BackColor = Color.SkyBlue
            ElseIf DataGridListeDep.Rows(i).Cells(11).Value.ToString() = "en attente piece" Then
                DataGridListeDep.Rows(i).DefaultCellStyle.BackColor = Color.Yellow
            ElseIf DataGridListeDep.Rows(i).Cells(11).Value.ToString() = "en attente confirmation" Then
                DataGridListeDep.Rows(i).DefaultCellStyle.BackColor = Color.Orange
            ElseIf DataGridListeDep.Rows(i).Cells(11).Value.ToString() = "fin depannage" Then
                DataGridListeDep.Rows(i).DefaultCellStyle.BackColor = Color.Green
            ElseIf DataGridListeDep.Rows(i).Cells(11).Value.ToString() = "deplacement" Then
                DataGridListeDep.Rows(i).DefaultCellStyle.BackColor = Color.Pink
            ElseIf DataGridListeDep.Rows(i).Cells(11).Value.ToString() = "vente" Then
                DataGridListeDep.Rows(i).DefaultCellStyle.BackColor = Color.Violet
            End If
            i = i + 1
        End While

        CacherColonne(DataGridListeDep, "EtatIntervention")
        CacherColonne(DataGridListeDep, "idclient")
        CacherColonne(DataGridListeDep, "livraison")
        CacherColonne(DataGridListeDep, "Fin_Intervention")
        stat = False

    End Sub

    Public Sub CacherColonne(data As DataGridView, colonne As String)

        data.Columns(colonne).Visible = False

    End Sub

    Public Sub connection()
        macnx.Se_Connecter_MySqlServer()
        DataGridListeDep.DataSource = Nothing
        macnx.viderVue("IntEnCour")
        DataGridListeDep.DataSource = macnx.Executer_Requete_Select("SELECT intervention.idIntervention AS Numero , nomClient AS Nom, PrenomClient AS Prenom, TypeIntervention, TypeMateriel, Lieu, date.DateHeureReception AS Reception, date.dateHeureDebIntervention AS Debut, Date.dateHeureFinIntervention AS Fin_Intervention, date.DateHeureLivraison AS Livraison, client.idclient , EtatIntervention FROM intervention, client, DATE WHERE ( intervention.idCli = client.idClient ) AND ( client.idClient = date.idcli ) AND ( intervention.idintervention = date.idIntervention ) AND ( etatIntervention =  'en cours' OR etatIntervention =  'en reception' OR etatIntervention =  'en attente confirmation' OR etatIntervention =  'en attente piece'  OR etatIntervention =  'en depannage' OR etatIntervention =  'fin depannage' OR etatIntervention = 'deplacement' OR etatIntervention = 'vente' ) ORDER BY intervention.idIntervention asc", "IntEnCour")
        macnx.Se_Deconnecter_MySqlServer()
        DataGridListeDep.AutoResizeColumns()
    End Sub

#End Region

    Private Sub BtnValider_Click(sender As System.Object, e As System.EventArgs) Handles BtnValider.Click

        Dim nom As String = TextBoxNomClient.Text
        Dim prenom As String = TextBoxPrenomClient.Text
        Message = "Voulez-vous metre fin a l'intervention numéro " & lblIdIntervention.Text & " du client " & prenom & " " & nom
        Titre = "Attention !"
        If MessageBox.Show(Message, Titre, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then

            Dim terminée As String = "terminée"

            macnx.Se_Connecter_MySqlServer()
            macnx.Executer_Requete_Action("update intervention set EtatIntervention = '" & terminée & "'  where IdIntervention = " & lblIdIntervention.Text & "")
            macnx.Executer_Requete_Action("UPDATE date SET DateHeureLivraison  = NOW()  WHERE IdIntervention = " & lblIdIntervention.Text & "")

            macnx.Se_Deconnecter_MySqlServer()

            macnx.viderVue("switchInter")
            FrmDepTerm.DataGridListeDep.DataSource = macnx.Executer_Requete_Select("select intervention.idIntervention as numero_intervention,nomClient,PrenomClient,TypeIntervention,TypeMateriel,Lieu,date.DateHeureReception as Reception, date.dateHeureDebIntervention as Debut_Intervention, Date.dateHeureFinIntervention as Fin_Intervention, date.DateHeureLivraison as Livraison, client.idclient from intervention, client, date where intervention.idCli = client.idClient and client.idClient = date.idcli and intervention.idintervention = date.idIntervention and etatIntervention = 'terminée'", "switchInter")

            MessageBox.Show("L'intervention sélectionnée est terminée", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            FrmRecapAjoutDep.LblNom.Text = nom
            FrmRecapAjoutDep.LblPrenom.Text = prenom
            FrmTicket.lblNumInter.Text = lblIdIntervention.Text
            FrmTicket.lblNumClient.Text = lblidClient.Text
            FrmRecapAjoutDep.create = False
            FrmTicket.Show()

            FrmRecapAjoutDep.Show()

            connection()
            rafraichir()
        End If
    End Sub

#Region "Cell Click"
    Private Sub DataGridListeDep_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridListeDep.CellClick
        rafraichir()
    End Sub
#End Region

    Private Sub PictureBox1_Click(sender As System.Object, e As System.EventArgs) Handles PictureBox1.Click
        connection()

        rafraichir()
    End Sub

#Region "CheckBox"

    Private Sub CheckBoxEnDepannage_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxEnDepannage.CheckedChanged
        If stat = False Then
            If CheckBoxEnDepannage.Enabled = True Then
                Message = "Voulez-vous metre " & TextBoxPrenomClient.Text & " " & TextBoxNomClient.Text & " en état de dépannage ?"
                Titre = "Attention !"
                If MessageBox.Show(Message, Titre, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                    macnx.Se_Connecter_MySqlServer()
                    macnx.Executer_Requete_Action("UPDATE intervention SET EtatIntervention = 'en depannage'  WHERE IdIntervention = " & lblIdIntervention.Text & "")
                    macnx.Executer_Requete_Action("UPDATE date SET DateHeureDebIntervention = NOW()  WHERE IdIntervention = " & lblIdIntervention.Text & "")
                    macnx.Se_Deconnecter_MySqlServer()
                    CheckBoxAttenteC.Checked = False
                    CheckBoxAttenteP.Checked = False
                    CheckBoxEnCour.Checked = False
                    CheckBoxFinDepannage.Checked = False
                    CheckBoxAttenteC.Enabled = True
                    CheckBoxAttenteP.Enabled = True
                    CheckBoxFinDepannage.Enabled = True
                    CheckBoxEnDepannage.Enabled = False

                End If

            End If
        End If
    End Sub

    Private Sub CheckBoxAttenteC_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxAttenteC.CheckedChanged
        If stat = False Then
            If CheckBoxEnDepannage.Enabled = False Then
                If (CheckBoxAttenteC.Checked = True) Then
                    Message = "Voulez-vous metre " & TextBoxPrenomClient.Text & " " & TextBoxNomClient.Text & " en état d'attente de confirmation ? "
                    Titre = "Attention !"
                    If MessageBox.Show(Message, Titre, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                        macnx.Se_Connecter_MySqlServer()
                        macnx.Executer_Requete_Action("UPDATE intervention SET EtatIntervention = 'en attente confirmation'  WHERE IdIntervention = " & lblIdIntervention.Text & "")
                        macnx.Se_Deconnecter_MySqlServer()
                        CheckBoxAttenteP.Checked = False

                    End If
                ElseIf (CheckBoxAttenteC.Checked = False) Or (CheckBoxAttenteP.Checked = False) Then
                    Message = "Voulez-vous metre " & TextBoxPrenomClient.Text & " " & TextBoxNomClient.Text & " en état de dépannage ?"
                    Titre = "Attention !"
                    If MessageBox.Show(Message, Titre, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                        macnx.Se_Connecter_MySqlServer()
                        macnx.Executer_Requete_Action("UPDATE intervention SET EtatIntervention = 'en depannage'  WHERE IdIntervention = " & lblIdIntervention.Text & "")
                        macnx.Se_Deconnecter_MySqlServer()
                        CheckBoxEnDepannage.Enabled = False
                        CheckBoxEnCour.Checked = False


                    End If
                End If
            End If
            CheckBoxFinDepannage.Enabled = True
            CheckBoxFinDepannage.Checked = False
            CheckBoxEnDepannage.Enabled = False
        End If
    End Sub

    Private Sub CheckBoxAttenteP_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxAttenteP.CheckedChanged
        If stat = False Then
            If CheckBoxEnDepannage.Enabled = False And CheckBoxFinDepannage.Checked = False Then
                If CheckBoxAttenteP.Checked = True Then
                    Message = "Voulez-vous metre " & TextBoxPrenomClient.Text & " " & TextBoxNomClient.Text & " en état d'attente de pièce ? "
                    Titre = "Attention !"
                    If MessageBox.Show(Message, Titre, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then
                        macnx.Se_Connecter_MySqlServer()
                        macnx.Executer_Requete_Action("UPDATE intervention SET EtatIntervention = 'en attente piece'  WHERE IdIntervention = " & lblIdIntervention.Text & "")
                        macnx.Se_Deconnecter_MySqlServer()
                        CheckBoxAttenteC.Checked = False

                    End If
                ElseIf ((CheckBoxAttenteC.Checked = False) Or (CheckBoxAttenteP.Checked = False)) And (CheckBoxFinDepannage.Enabled = True) Then
                    Message = "Voulez-vous metre " & TextBoxPrenomClient.Text & " " & TextBoxNomClient.Text & " en état de dépannage ? "
                    Titre = "Attention !"
                    If MessageBox.Show(Message, Titre, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then

                        macnx.Se_Connecter_MySqlServer()
                        macnx.Executer_Requete_Action("UPDATE intervention SET EtatIntervention = 'en depannage'  WHERE IdIntervention = " & lblIdIntervention.Text & "")
                        macnx.Se_Deconnecter_MySqlServer()
                        CheckBoxEnDepannage.Enabled = False
                        CheckBoxEnCour.Checked = False
                        macnx.Se_Connecter_MySqlServer()
                        DataGridListeDep.DataSource = Nothing
                        macnx.viderVue("listeInt")
                        DataGridListeDep.DataSource = macnx.Executer_Requete_Select("SELECT intervention.idIntervention AS numero_intervention, nomClient, PrenomClient, TypeIntervention, TypeMateriel, Lieu, date.DateHeureReception AS Reception, date.dateHeureDebIntervention AS Debut_Intervention, Date.dateHeureFinIntervention AS Fin_Intervention, date.DateHeureLivraison AS Livraison, client.idclient , EtatIntervention FROM intervention, client, DATE WHERE ( intervention.idCli = client.idClient ) AND ( client.idClient = date.idcli ) AND ( intervention.idintervention = date.idIntervention ) AND ( etatIntervention =  'en cours' OR etatIntervention =  'en reception' OR etatIntervention =  'en attente confirmation' OR etatIntervention =  'en attente piece'  OR etatIntervention =  'en depannage' OR etatIntervention =  'fin depannage' ) ORDER BY intervention.idIntervention asc", "listeInt")
                        macnx.Se_Deconnecter_MySqlServer()
                        DataGridListeDep.AutoResizeColumns()

                    End If
                End If
            End If
            CheckBoxFinDepannage.Enabled = True
            CheckBoxFinDepannage.Checked = False
            CheckBoxEnDepannage.Enabled = False
        End If
    End Sub

    Private Sub CheckBoxFinDepannage_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles CheckBoxFinDepannage.CheckedChanged
        If stat = False Then
            If CheckBoxFinDepannage.Enabled = True And CheckBoxFinDepannage.Checked = True Then

                Message = "Voulez-vous metre " & TextBoxPrenomClient.Text & " " & TextBoxNomClient.Text & " en état de fin de dépannage ? "
                Titre = "Attention !"
                If MessageBox.Show(Message, Titre, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = Windows.Forms.DialogResult.Yes Then

                    macnx.Se_Connecter_MySqlServer()
                    macnx.Executer_Requete_Action("UPDATE intervention SET EtatIntervention = 'fin depannage'  WHERE IdIntervention = " & lblIdIntervention.Text & "")
                    macnx.Executer_Requete_Action("UPDATE date SET DateHeureFinIntervention = NOW()  WHERE IdIntervention = " & lblIdIntervention.Text & "")
                    macnx.Se_Deconnecter_MySqlServer()
                    CheckBoxAttenteP.Checked = False
                    CheckBoxEnDepannage.Enabled = False
                    CheckBoxEnCour.Checked = False
                    CheckBoxAttenteC.Checked = False
                    CheckBoxEnDepannage.Checked = False
                    CheckBoxAttenteP.Enabled = False
                    CheckBoxAttenteC.Enabled = False
                    CheckBoxFinDepannage.Enabled = True
                    macnx.viderVue("EndInt")
                    FrmDepTerm.DataGridListeDep.DataSource = macnx.Executer_Requete_Select("select intervention.idIntervention as numero_intervention,nomClient,PrenomClient,TypeIntervention,TypeMateriel,Lieu,date.DateHeureReception as Reception, date.dateHeureDebIntervention as Debut_Intervention, Date.dateHeureFinIntervention as Fin_Intervention, date.DateHeureLivraison as Livraison, client.idclient from intervention, client, date where intervention.idCli = client.idClient and client.idClient = date.idcli and intervention.idintervention = date.idIntervention and etatIntervention = 'terminée'", "EndInt")

                    MessageBox.Show("L'intervention sélectionnée est terminée", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                    FrmRecapAjoutDep.LblNom.Text = TextBoxNomClient.Text
                    FrmRecapAjoutDep.LblPrenom.Text = TextBoxPrenomClient.Text
                    FrmRecapAjoutDep.create = False
                    FrmTravauxEffectues.Show()
                    FrmRecapAjoutDep.Show()
                End If
            End If
        End If
    End Sub

#End Region

    Private Sub DataGridListeDep_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridListeDep.CellDoubleClick
        FrmRecapAjoutDep.LblNom.Text = TextBoxNomClient.Text
        FrmRecapAjoutDep.LblPrenom.Text = TextBoxPrenomClient.Text
        FrmRecapAjoutDep.create = False
        FrmTravauxEffectues.Show()
        FrmRecapAjoutDep.Show()

    End Sub
End Class